<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/operatehistory.jsp"%>
<%!
	
	public String category(Input input) {
		return category(input, -1);
	}
	
	public String category(Input input, Integer uid) {
		Message msg = new Message();
		Integer parent = input.getInt("parent", -1);	 // 父类id,传0表示既没有大类也没有小类
		Integer hospital = input.getInt("hospital", -1); // 医院id
		Integer category = input.getInt("category", -1); // 大分类  1新闻的标签 2问答标签 3科室 4医生 5病情备忘 6日程 7新闻问答的关联偏好
		Integer type = input.getInt("type", -1);        // 小分类
		Integer branch = input.getInt("branch", -1);        // 所属分医院
		Integer level = input.getInt("level", -1);        // 标签是否需要多级分类默认不需要
		Where where = new Where();
		String table = "Label as l";
		if (type != -1) {
			where.and("l.type=", type);
		}
		if (category != -1) {
			where.and("l.category=", category);
		}
		if (branch != -1) {
			where.and("l.branch=", branch);
		}
		if (uid != -1) {
			where.and("o.uid=", uid).and("o.operate=" + ATTENTION).and("o.category=" + LABEL_CATEGORY).and("o.oid=l.id").and("o.status=", STATUS_ENABLE);
			table = table + ", OperateHistory as o";
		}
		List<Map> list = QBuilder.db("basedb").select("new map(l.id as id, l.name as name, l.content as content,  l.queue as queue,  l.icon as icon,  l.parent as parent,   l.branch as branch,  l.type as category)").from(table).where(where.and("l.hospital=", hospital).and("l.status=", STATUS_ENABLE).and("l.enable=", STATUS_ENABLE)).order("l.queue", "asc").query().getResultObj();
		if (list.size() == 0) {
			msg = new Message(30041, "暂无相关数据");
			msg.put("data", list);
		} else {
			List<Map> dest = new ArrayList<Map>();
			Iterator<Map> iterator = list.iterator();
			if(level > 0) {  // 大于0需要多级分类
				List<Map> _dest = new ArrayList<Map>();
				while (iterator.hasNext()) {
					Map map = iterator.next();
					map.put("size", "0");
					map.put("sub", new ArrayList());
					Integer _parent = Tools.getInt(map.get("parent")+"", -1000000);
					if (_parent.equals(parent)) {
						iterator.remove();
						_dest.add(map);
						List<Map> _list = new ArrayList<Map>();
						_list.addAll(list);
						dest.add(cate(_list, map, _dest));
					}
				}
				list.removeAll(_dest);
				dest.addAll(list);
			} else {      // 小于等于0只需最后不同分类
				List<String> ids = new ArrayList<String>();
				List<String> parents = new ArrayList<String>();
				while (iterator.hasNext()) {
					Map map = iterator.next();
					ids.add(Tools.getDesc(map.get("id")));
					parents.add(Tools.getDesc(map.get("parent")));
				}
				ids.removeAll(parents);
				for (int i = 0; i < ids.size(); i++) {
					String id = ids.get(i);
					for (int j = 0; j < list.size(); j++) {
						Map map = list.get(j);
						String _id = Tools.getDesc(map.get("id"), "-------");
						if (_id.equals(id)) {
							dest.add(map);
							break;
						}
					}
				}
			}
			sort(dest);
			if (dest.size() == 0)
				msg = new Message(30041, "暂无相关数据");
			msg.put("data", dest);
		}
		
		return msg.toJson();
	}
	
	// 递归遍历标签
	public Map cate(List<Map> list, Map dest, List<Map> _dest) {
		List<Map> subs = (List<Map>)dest.get("sub");
		if (subs == null) {
			subs = new ArrayList<Map>();
			dest.put("sub", subs);
		}
		Integer _parent = Tools.getInt(dest.get("id")+"", -1001);
		Iterator<Map> iterator = list.iterator();
		while (iterator.hasNext()) {
			Map map = iterator.next();
			Integer parent = Tools.getInt(map.get("parent")+"", -1000);
			if (_parent.equals(parent)) {
			iterator.remove();
			_dest.add(map);
			List<Map> _list = new ArrayList<Map>();
			_list.addAll(list);
			subs.add(cate(_list, map, _dest));
			}
		}
		sort(subs);
		dest.put("size", subs.size() + "");
		return dest;
	}

	// 排序
	public void sort(List<Map> list) {
		Collections.sort(list, new Comparator<Map>(){
			public int compare(Map n1, Map n2) {
				return Tools.getInt(n1.get("queue")+"", -1) - Tools.getInt(n2.get("queue")+"", -1);
			}
		});
	}
%>